﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace AnyFileManagement.UI.Controls
{
    class TextBox:UserControl
    {
        public System.Windows.Forms.TextBox txt_text;
        private Padding _padding = new Padding(10, 0, 10, 0); 
        private Color _staticColor = Color.FromArgb(255, 255, 255);
        private Color _hoverColor = Color.FromArgb(255, 255, 255);
        private Color _focusColor = Color.FromArgb(255, 255, 255);
        private float _colorDecay = 0.8f;
        private string _value;
        private string _blankText;
        private Color _blankTextColor = Color.FromArgb(189, 195, 199);
        private Color _staticBorderColor = Color.FromArgb(243, 156, 18);
        private Color _hoverBorderColor = Color.FromArgb(246, 181, 23);
        private Color _focusBorderColor = Color.FromArgb(246, 181, 23);
        private int _borderSize = 2;
        private bool _isMouseEnter=false;
        private bool _isFocus = false;
        private Theme _theme = Theme.D3D;
        private int _extrude = 2;
        private Image _image;
        private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
        private ContentAlignment _imageAlign = ContentAlignment.MiddleCenter;
        private int _radian=5;
        private int _offsetXPer = 1;
        private int _offsetYper = 1;

        /// <summary>
        /// 角度X偏移量
        /// </summary>
        [Category("JUI外观"), Description("角度X偏移量")]
        public int OffsetXPer
        {
            get { return _offsetXPer; }
            set { _offsetXPer = value; }
        }

        /// <summary>
        /// 角度Y偏移量
        /// </summary>
        [Category("JUI外观"), Description("角度Y偏移量")]
        public int OffsetYper
        {
            get { return _offsetYper; }
            set { _offsetYper = value; }
        }

        /// <summary>
        /// 圆角弧度值
        /// </summary>
        [Category("JUI外观"), Description("圆角弧度值")]
        public int Radian
        {
            get { return _radian; }
            set { _radian = value; }
        }


        /// <summary>
        /// 当主题为D3D模式时，挤出高度值
        /// </summary>
        [Category("JUI外观"), Description("当主题为D3D模式时，挤出高度值")]
        public int Extrude
        {
            get { return _extrude; }
            set { _extrude = value; }
        }

        /// <summary>
        /// 图片对齐方式
        /// </summary>
        [Category("JUI外观"), Description("图片对齐方式")]
        public ContentAlignment ImageAlign
        {
            get { return _imageAlign; }
            set { _imageAlign = value; }
        }


        /// <summary>
        /// 图片
        /// </summary>
        [Category("JUI外观"), Description("图片")]
        public Image Image
        {
            get { return _image; }
            set { _image = value; }
        }

        /// <summary>
        /// 颜色衰减值
        /// </summary>
        [Category("JUI外观"), Description("颜色衰减值")]
        public float ColorDecay
        {
            get { return _colorDecay; }
            set { _colorDecay = value; }
        }

        /// <summary>
        /// 主题
        /// </summary>
        [Category("JUI外观"), Description("主题")]
        public Theme Theme
        {
            get { return _theme; }
            set { _theme = value; }
        }

        /// <summary>
        /// 文字对齐方式
        /// </summary>
        [Category("JUI外观"), Description("文字对齐方式")]
        public HorizontalAlignment TextAlign
        {
            get { return _textAlign; }
            set
            {
                _textAlign = value;
                this.txt_text.TextAlign = _textAlign;
            }
        }

        /// <summary>
        /// 边框粗细
        /// </summary>
        [Category("JUI外观"), Description("边框粗细")]
        public int BorderSize
        {
            get { return _borderSize; }
            set { _borderSize = value; }
        }

        /// <summary>
        /// 获得焦点后的边框颜色
        /// </summary>
        [Category("JUI外观"), Description("获得焦点后的边框颜色")]
        public Color FocusBorderColor
        {
            get { return _focusBorderColor; }
            set { _focusBorderColor = value; }
        }

        /// <summary>
        /// 焦点边框颜色
        /// </summary>
        [Category("JUI外观"), Description("焦点边框颜色")]
        public Color HoverBorderColor
        {
            get { return _hoverBorderColor; }
            set { _hoverBorderColor = value; }
        }

        /// <summary>
        /// 默认边框颜色
        /// </summary>
        [Category("JUI外观"), Description("默认边框颜色")]
        public Color StaticBorderColor
        {
            get { return _staticBorderColor; }
            set { _staticBorderColor = value; }
        }

        /// <summary>
        /// 空值文本颜色
        /// </summary>
        [Category("JUI外观"), Description("空值文本颜色")]
        public Color BlankTextColor
        {
            get { return _blankTextColor; }
            set { _blankTextColor = value; }
        }

        /// <summary>
        /// 空值时显示的文本
        /// </summary>
        [Category("JUI外观"), Description("空值时显示的文本")]
        public string BlankText
        {
            get { return _blankText; }
            set { _blankText = value; }
        }

        /// <summary>
        /// 文本值
        /// </summary>
        [Category("JUI外观"), Description("文本值")]
        public string Value
        {
            get { return _value; }
            set { 
                _value = value;
                this.txt_text.Text = _value;
            }
        }
        
        /// <summary>
        /// 常规颜色
        /// </summary>
        [Category("JUI外观"), Description("常规颜色")]
        public Color StaticColor
        {
            get { return _staticColor; }
            set { _staticColor = value; }
        }

        /// <summary>
        /// 焦点颜色
        /// </summary>
        [Category("JUI外观"), Description("焦点颜色")]
        public Color HoverColor
        {
            get { return _hoverColor; }
            set { _hoverColor = value; }
        }

        /// <summary>
        /// 获得焦点后的颜色
        /// </summary>
        [Category("JUI外观"), Description("获得焦点后的颜色")]
        public Color FocusColor
        {
            get { return _focusColor; }
            set { _focusColor = value; }
        }


        /// <summary>
        /// 构造
        /// </summary>
        public TextBox()
        {
            //this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            //InitializeComponent();

            //BackColor = Color.Transparent;

            //SetWindowRgn(this.Handle, CreateRoundRectRgn(5, 5, this.Width - 1, this.Height - 1, this.Width - 1, this.Height - 1),true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.BorderStyle = BorderStyle.None;
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;
            //this.Region = new Region(DrawingHelper.CreateRoundedRectanglePath(new Rectangle(1,1,this.Width-2,this.Height-2),8));
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.Font = new System.Drawing.Font("Arial", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, (byte)134);
            txt_text = new System.Windows.Forms.TextBox();
            txt_text.BorderStyle = BorderStyle.None;
            txt_text.BackColor = _staticColor;
            txt_text.Text = _value;
            txt_text.GotFocus += new EventHandler((o, e) =>
            {
                _isFocus = true;
                if (txt_text.Text.Length == 0 || txt_text.Text.Equals(_blankText))
                {
                    txt_text.Text = ""; 
                    txt_text.ForeColor = this.ForeColor;
                }
            });
            txt_text.LostFocus += new EventHandler((o, e) =>
            {
                _isFocus = false;
                if (txt_text.Text.Length == 0 || txt_text.Text.Equals(_blankText))
                {
                    txt_text.Text = _blankText; 
                    txt_text.ForeColor = _blankTextColor;
                }
            }); 
            this.txt_text.MouseEnter += new EventHandler((o, e) => { this.OnMouseEnter(e); });
            this.txt_text.MouseLeave += new EventHandler((o, e) => { this.OnMouseLeave(e); });
            this.txt_text.MouseUp += new System.Windows.Forms.MouseEventHandler((o, e) => { this.OnMouseUp(e); });
            this.txt_text.MouseDown += new System.Windows.Forms.MouseEventHandler((o, e) => { this.OnMouseDown(e); });
            this.txt_text.TextChanged += new EventHandler((o, e) => { this.OnTextChanged(e); });
            this.txt_text.DoubleClick += new EventHandler((o, e) => { this.OnDoubleClick(e);  });
            this.txt_text.KeyDown += new KeyEventHandler((o, e) => { this.OnKeyDown(e); });
            this.txt_text.KeyUp += new KeyEventHandler((o, e) => { this.OnKeyUp(e);  });
            this.txt_text.KeyPress +=new KeyPressEventHandler((o, e) => { this.OnKeyPress(e); });
            this.Controls.Add(txt_text);            
        }
        
        protected override void OnTextChanged(EventArgs e)
        {
            this._value = this.txt_text.Text;
            base.OnTextChanged(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            _isMouseEnter = true;
            this.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            _isMouseEnter = false;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            txt_text.SetBounds(this.Height/2, this.Height/2-txt_text.Height/2+_borderSize/2, this.Width-this.Height, this.Height);
            if (!_isFocus)
            {
                txt_text.Text = string.IsNullOrEmpty(txt_text.Text) ? _blankText : this.txt_text.Text;
                txt_text.ForeColor = string.IsNullOrEmpty(txt_text.Text) || txt_text.Text.Equals(_blankText) ? _blankTextColor : this.ForeColor;
            }
            //DrawingHelper.FillRoundRectangle(g, new SolidBrush(Color.RosyBrown), new Rectangle(0, 0, this.Width - 1, this.Height - 1), 8);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            Graphics g = pevent.Graphics;
            Rectangle r = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            Rectangle b=new Rectangle();
            Color currentColor = _staticColor;
            Color currentBorderColor = _staticBorderColor;
            if (_isFocus)
            {
                currentColor = _focusColor;
                currentBorderColor = _focusBorderColor;
            }
            else
            {
                if (_isMouseEnter)
                {
                    currentColor = _hoverColor;
                    currentBorderColor = _hoverBorderColor;
                }
            }
            if (_borderSize > 0)
            {
                b = new Rectangle(_borderSize / 2, _borderSize / 2, this.Width - _borderSize-1, this.Height - _borderSize-1);
                r = b;
            }
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            
            //判断主题模式
            switch (_theme)
            {
                case Theme.Static:
                    //绘制背景色
                    DrawingHelper.FillRoundRectangle(g, new SolidBrush(currentColor), r, _offsetXPer,_offsetYper, _radian);
                    this.txt_text.BackColor = currentColor;
                    if (_borderSize > 0)
                    {
                        //绘制边框
                        DrawingHelper.DrawRoundRectangle(g, new Pen(currentBorderColor, _borderSize), b, _offsetXPer,_offsetYper, _radian);
                    }
                    ; break;
                case Theme.D3D:
                    Color bordercolor = _borderSize > 0 ? Color.FromArgb((int)(currentBorderColor.R * ColorDecay), (int)(currentBorderColor.G * ColorDecay), (int)(currentBorderColor.B * ColorDecay)) : Color.FromArgb((int)(currentColor.R * ColorDecay), (int)(currentColor.G * ColorDecay), (int)(currentColor.B * ColorDecay));
                    //绘制背景色
                    DrawingHelper.FillRoundRectangle(g, new SolidBrush(bordercolor), new Rectangle(0, 0, this.Width - 1, this.Height - 1), _offsetXPer, _offsetYper, _radian);
                    r.Height -= this._extrude;
                    b.Height -= this._extrude;
                    DrawingHelper.FillRoundRectangle(g, new SolidBrush(currentColor), r, _offsetXPer, _offsetYper, _radian);
                    this.txt_text.BackColor = currentColor;
                    if (_borderSize > 0)
                    {
                        //绘制边框
                        DrawingHelper.DrawRoundRectangle(g, new Pen(currentBorderColor, _borderSize), b, _offsetXPer, _offsetYper, _radian);
                    }                    
                    ; break;
            }


        }

       /* protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0xf || m.Msg == 0x14 || m.Msg == 0x85)
            {
                using(Graphics g=Graphics.FromHwnd(this.Handle)){
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    using(Pen pen=new Pen(Color.Red)){
                        DrawingHelper.FillRoundRectangle(g, new SolidBrush(Color.Red), new Rectangle(0, 0, this.Width - 1, this.Height - 1), 5);
                        //g.DrawRectangle(pen,0,0,this.Width-1,this.Height-1);
                    }
                }
            }
        }*/

    }
}
